<?php
/**
 * OrangeHRM is a comprehensive Human Resource Management (HRM) System that captures
 * all the essential functionalities required for any enterprise.
 * Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com
 *
 * OrangeHRM is free software; you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * OrangeHRM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA
 */

//$baseURL = "{$_SERVER['PHP_SELF']}?recruitcode={$_GET['recruitcode']}";

require_once ROOT_PATH . '/lib/models/eimadmin/JobTitle.php';
require_once ROOT_PATH . '/lib/models/eimadmin/Skills.php';
require_once ROOT_PATH . '/lib/models/eimadmin/LanguageInfo.php';
$baseURL = "{$_SERVER['PHP_SELF']}?recruitcode={$_GET['recruitcode']}";
$detailsURL = $baseURL . '&action=ViewDetails';
$historyURL = $baseURL . '&action=ViewHistory';
$firstEvaluationURL = $baseURL . '&action=ViewFirstEvaluation';
$secondEvaluationURL = $baseURL . '&action=ViewSecondEvaluation';

$srchlist = array( "-$lang_Leave_Common_Select-" , "Name" );

$applications = $records['applications'];

$locRights = $_SESSION['localRights'];

$currentPage = $records['page'];

$positions = JobVacancy::getAll();
		
$lang = new LanguageInfo();
$langList = $lang->getLang();

$skill = new Skills();
$skillList = $skill->getSkillCodes();

//$currentPage = $this->popArr['currentPage'];

//$list = $this->popArr['list'];
//$baseURL = './CentralController.php?recruitcode='. $this->getArr['recruitcode'];//Chinh Nguyen command this
//$baseURL = './CentralController.php?newscode='. $this->getArr['newscode'];

$allowAdd = $locRights['add'];
$allowDelete = $locRights['delete'];

$themeDir = '../../themes/' . $styleSheet;

$statusList = array(
    JobApplication::STATUS_SUBMITTED => $lang_Recruit_JobApplicationStatus_Submitted,
    JobApplication::STATUS_FIRST_INTERVIEW_SCHEDULED => $lang_Recruit_JobApplicationStatus_FirstInterview,
    JobApplication::STATUS_SECOND_INTERVIEW_SCHEDULED => $lang_Recruit_JobApplicationStatus_SecondInterview,
    JobApplication::STATUS_JOB_OFFERED => $lang_Recruit_JobApplicationStatus_JobOffered,
    JobApplication::STATUS_OFFER_DECLINED => $lang_Recruit_JobApplicationStatus_OfferDeclined,
    JobApplication::STATUS_PENDING_APPROVAL => $lang_Recruit_JobApplicationStatus_PendingApproval,
    JobApplication::STATUS_HIRED => $lang_Recruit_JobApplicationStatus_Hired,
    JobApplication::STATUS_REJECTED => $lang_Recruit_JobApplicationStatus_Rejected,
    JobApplication::STATUS_ACCEPTED => $lang_Recruit_JobApplicationStatus_Accepted,
    JobApplication::STATUS_UN_ACCEPTED => $lang_Recruit_JobApplicationStatus_Unaccepted,
    JobApplication::STATUS_RESETED => $lang_Recruit_JobApplicationStatus_Reseted,
   JobApplication::STATUS_APPROVED => $lang_Recruit_JobApplicationStatus_Approved,
    JobApplication::STATUS_HIRED => $lang_Recruit_JobApplicationStatus_Hired
    );

?>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="../../scripts/archive.js"></script>
<script type="text/javascript" src="../../scripts/octopus.js"></script>
<script type="text/javascript">
function returnAdd() {

		location.href = '<?php echo $baseURL;?>&action=ViewAdd';
	}
	function resetForm() {
		$('standardView').reset();
		document.getElementById('position').value=null;
		document.getElementById('skill').value=null;
		document.getElementById('lang').value=null;
		document.getElementById('experience').value=null;
		document.getElementById('status').value=null;
		return returnSearch();
		
	}

	
</script>
<link href="../../themes/<?php echo $styleSheet;?>/css/style.css" rel="stylesheet" type="text/css">
<style type="text/css">@import url("../../themes/<?php echo $styleSheet;?>/css/style.css"); </style>

<style type="text/css">
    <!--
    .roundbox {
        margin-top: 10px;
        margin-left: 0px;
        width:98%;
    }

    .roundbox_content {
        padding:15px 15px 15px 15px;
    }
    .eventDate {
        font-style: italic;
    }
    -->
</style>
</head>
<script type="text/javascript">
	function nextPage() {
		var i=eval(document.standardView.pageNO.value);
		document.standardView.pageNO.value=i+1;
		document.standardView.submit();
	}

	function prevPage() {
		var i=eval(document.standardView.pageNO.value);
		document.standardView.pageNO.value=i-1;
		document.standardView.submit();
	}

	function chgPage(pNO) {
		document.standardView.pageNO.value=pNO;
		document.standardView.submit();
	}

	

	function returnAdd() {
		//alert($baseURL);
		location.href = '<?php echo $baseURL;?>&action=ViewAdd';
	}

	function returnDelete() {
		
		var check = false;
		with (document.standardView) {
			for (var i=0; i < elements.length; i++) {
				if ((elements[i].type == 'checkbox') && (elements[i].checked == true) && (elements[i].name == 'chkID[]')){
					check = true;
					break;
				}
			}
		}

		if (check){

			var res = confirm("<?php echo $lang_Common_ConfirmDelete; ?>");
			if (!res) {
			    return false;
			}

			document.standardView.action="<?php echo $baseURL;?>&action=Delete";
			
			document.standardView.submit();
		} else {
			alert("<?php echo $lang_Common_SelectDelete; ?>");
		}
	}

	

	function doHandleAll() {
		with (document.standardView) {
			if(elements['allCheck'].checked == false){
				doUnCheckAll();
			}
			else if(elements['allCheck'].checked == true){
				doCheckAll();
			}
		}
	}

	function doCheckAll() {
		with (document.standardView) {
			for (var i=0; i < elements.length; i++) {
				if (elements[i].type == 'checkbox') {
					elements[i].checked = true;
				}
			}
		}
	}
	function returnSearch() {
		/*
		searchBy = $('loc_code');

		if (searchBy.options[searchBy.selectedIndex].value == -1) {
			alert("<?php echo $lang_Common_SelectField; ?>");
			searchBy.focus();
			return;
		};
		searchNdx = searchBy.options[searchBy.selectedIndex].value;
		var searchVal = $('loc_name').value;

		if (searchNdx == 3) {
		    map = maps[searchNdx];
		    if (searchVal in map) {
		        $('loc_name').value = map[searchVal];
		    } else {
		        var len = map.length;
		        var allowed = '';
		        for ( var i in map) {
		        	if (allowed == ''){
		            	allowed = i;
		        	} else {
		            	allowed = allowed + ', ' + i;
		            }
		        }
		        alert("<?php echo $lang_Recruit_AllowedValuesAre;?> " + allowed);
		        return;
		    }
		}
		*/
		document.standardView.captureState.value = 'SearchMode';
		document.standardView.pageNO.value=1;
		document.standardView.submit();
	}

	function doUnCheckAll() {
		with (document.standardView) {
			for (var i=0; i < elements.length; i++) {
				if (elements[i].type == 'checkbox') {
					elements[i].checked = false;
				}
			}
		}
	}

	function clear_form() {
		document.standardView.loc_code.options[0].selected=true;
		document.standardView.loc_name.value='';
	}
</script>
<body>
<form name="standardView" id="standardView" method="post" action="<?php echo $baseURL;?>">
		<input type="hidden" name="captureState" value="<?php echo isset($this->postArr['captureState'])?$this->postArr['captureState']:''?>" />
		<input type="hidden" name="pageNO" value="<?php echo isset($records['page'])?$records['page']:'1'?>" />
		<table width='100%' cellpadding='0' cellspacing='0' border='0' class='moduleTitle'>
			<tr>
		  		<td width='100%'>
		  			<h2><?php echo $lang_Recruit_JobApplicationList_Heading; ?></h2>
		  		</td>
	  			<td valign='top' align='right' nowrap style='padding-top:3px; padding-left: 5px;'></td>
	  		</tr>
		</table>

		<div style="padding: 3px;">
		<?php	if($allowAdd) { ?>
		<img
			style="border: none"
			title="Add"
			alt="Add"
			src="<?php echo $themeDir;?>/pictures/btn_add.gif"
			onclick="returnAdd();"
			onmouseout="this.src='<?php echo $themeDir;?>/pictures/btn_add.gif';"
			onmouseover="this.src='<?php echo $themeDir;?>/pictures/btn_add_02.gif';" />
		<?php	} ?>

		<?php if($allowDelete) { ?>
		<img
			style="border: none"
	   		title="Delete"
	   		alt="Delete"
			src="<?php echo $themeDir;?>/pictures/btn_delete.gif"
			onclick="returnDelete();"
			onmouseout="this.src='<?php echo $themeDir;?>/pictures/btn_delete.gif';"
			onmouseover="this.src='<?php echo $themeDir;?>/pictures/btn_delete_02.gif';" />
		<?php } ?>
		</div>
    <?php $message =  isset($_GET['message']) ? $_GET['message'] : null;
    	if (isset($message)) {
			$col_def = CommonFunctions::getCssClassForMessage($message);
			$message = "lang_Common_" . $message;
	?>
	<div class="message">
		<font class="<?php echo $col_def?>" size="-1" face="Verdana, Arial, Helvetica, sans-serif">
			<?php echo (isset($$message)) ? $$message: ""; ?>
		</font>
	</div>
	<?php }	?>
  <div style="text-align: right; padding-top: 4px; width: 98%;" >
		<?php
		$temp = $records['count'];
		$commonFunc = new CommonFunctions();
		$pageStr = $commonFunc->printPageLinks($temp, $currentPage);
		$pageStr = preg_replace(array('/#first/', '/#previous/', '/#next/', '/#last/'), array($lang_empview_first, $lang_empview_previous, $lang_empview_next, $lang_empview_last), $pageStr);

		echo $pageStr;
		?>&nbsp;
	</div>
  	<div class="roundbox">
		<table  border="0" cellpadding="5" cellspacing="0" class="">
			<tr>
				
				<td width="200" class="dataLabel" style="white-space: nowrap">
					<label for="loc_name" style="float: left; padding-right: 10px;"><?php  echo "Search for:";?></label>
					<input type="text" size="20" name="loc_name" id="loc_name" class="dataField"  value="<?php echo $records['searchstr'];?>" />
				</td>
				<td align="right" width="50" class="dataLabel">
					<img
						title="Search"
						alt="Search"
						src="<?php echo $themeDir;?>/pictures/btn_search.gif"
						onclick="returnSearch();"
						onmouseover="this.src='<?php echo $themeDir;?>/pictures/btn_search_02.gif';"
						onmouseout="this.src='<?php echo $themeDir;?>/pictures/btn_search.gif';" />
				</td>
				<td>	
					<img
						title="Clear"
						alt="Clear"
						src="<?php echo $themeDir;?>/pictures/btn_clear.gif"
						onclick="resetForm();"
						onmouseover="this.src='<?php echo $themeDir;?>/pictures/btn_clear_02.gif';"
						onmouseout="this.src='<?php echo $themeDir;?>/pictures/btn_clear.gif';" />
						
				</td>
			</tr>
		</table>
		<table  border="0" cellpadding="0" cellspacing="0" class="">
			<tr>
				<td width="150" class="dataLabel">
					<label for="loc_code" style="float: left; padding-right: 10px;"><?php echo "Position:";?></label>
					<select style="z-index: 99;width:75px;" name="position" id="position" onchange="returnSearch();">
					<option value="" >-Select-</option>	
					<?php
						for($i=0;$i<count($positions);$i++) {
							if(isset($records['position']) && $records['position']==$positions[$i]->getJobTitleCode()) {							
								echo "<option selected value='" . $positions[$i]->getJobTitleCode() ."'>".$positions[$i]->getJobTitleName() ."</option>";
							} else {
								echo "<option value='" . $positions[$i]->getJobTitleCode() ."'>".$positions[$i]->getJobTitleName()  ."</option>";
							}
						}
					?>
					</select>
				</td>
				
				<td width="150" class="dataLabel">
					<label for="loc_code" style="float: left; padding-right: 10px;"><?php echo "Skill:";?></label>
					<select style="z-index: 99;width:75px;" name="skill" id="skill" onchange="returnSearch();">
					<option value="" >-Select-</option>	
					<?php
						for($j=0;$j<count($skillList);$j++) {
							if(isset($records['skill']) && $records['skill']==$skillList[$j][0]) {
								echo "<option selected value='" . $skillList[$j][0] ."'>".$skillList[$j][1]."</option>";
							} else {
								echo "<option value='" . $skillList[$j][0] ."'>".$skillList[$j][1]."</option>";
							}
						}
					?>
					</select>
				</td>
				<td width="150" class="dataLabel">
					<label for="loc_code" style="float: left; padding-right: 10px;"><?php echo "Language:";?></label>
					<select style="z-index: 99;width:75px;" name="lang" id="lang" onchange="returnSearch();">
					<option value="" >-Select-</option>	
					<?php
						for($k=0;$k<count($langList);$k++) {
							if(isset($records['lang']) && $records['lang']==$langList[$k][0]) {
								echo "<option selected value='" . $langList[$k][0] ."'>".$langList[$k][1]."</option>";
							} else {
								echo "<option value='" . $langList[$k][0] ."'>".$langList[$k][1]."</option>";
							}
						}
					?>
					</select>
				</td>
				<td width="150" class="dataLabel">
					<label for="loc_code" style="float: left; padding-right: 10px;"><?php echo "Experience:";?></label>
					<select style="z-index: 99;width:70px;" name="experience" id="experience" onchange="returnSearch();">
					<option value="" >-Select-</option>	
					<?php
						for($l=1;$l <= 10;$l++) {
							if(isset($records['experience']) && $records['experience']==$l) {
								echo "<option selected value='" . $l ."'>".$l." years" . "</option>";
							} else {
								echo "<option value='" . $l ."'>".$l." years"."</option>";
							}
						}
					?>
					</select>
				</td>
				<td width="150" class="dataLabel">
					<label for="loc_code" style="float: left; padding-right: 10px;"><?php echo "Status:";?></label>
					<select style="z-index: 99; width:75px;" name="status" id="status" onchange="returnSearch();">
					<option value="" >-Select-</option>	
					<?php
						for($n=0;$n < count($statusList);$n++) {
							if(isset($records['status']) && $records['status']==$n && $records['status']!='') {
																
								echo "<option selected value='" . $n ."'>".$statusList[$n] . "</option>";
							} else {
								echo "<option value='" . $n ."'>".$statusList[$n]."</option>";
							}
						}
					?>
					</select>
				</td>
				
			</tr>
		</table>
	</div>	
  <div class="roundbox">
    <?php if (count($applications) == 0) { ?>
        <?php echo $lang_Recruit_JobApplicationList_NoApplications;?>
    <?php } else { ?>
    
    
    <table width="100%" class="simpleList" border="0">
        <thead>
            <tr>
             <th>
				<?php	if($allowDelete) { ?>
					<input type='checkbox' class='checkbox' name='allCheck' value='' onclick="doHandleAll();" />
				<?php	} else {	?>
					&nbsp;
				<?php } ?>
			</th>
            <th width><?php echo $lang_Recruit_JobApplicationList_Name; ?></th>
            <th><?php echo "Job Title"; ?></th>
            <th><?php echo $lang_Recruit_HiringManager; ?></th>
            <th><?php echo $lang_Recruit_VacancyStatus; ?></th>
            <th><?php echo $lang_Recruit_JobApplicationList_Actions; ?></th>
            <th></th>
            </tr>
        </thead>
        <?php
            $odd = false;
            $authManager = new RecruitmentAuthManager();
            $authorize = new authorize($_SESSION['empID'], $_SESSION['isAdmin']);

            foreach ($applications as $app) {
                $cssClass = ($odd) ? 'even' : 'odd';
                $odd = !$odd;

                $applicantName = $app->getLastName().' '.$app->getMiddleName() . ' ' .$app->getFirstName() ;
                $applicationId = $app->getId();
                $status = $statusList[$app->getStatus()];

                $statusDate = '';
				
                $latestEvent = $app->getLatestEvent();
                
                if (!empty($latestEvent)) {
                    if (($latestEvent->getEventType() == JobApplicationEvent::EVENT_SCHEDULE_FIRST_INTERVIEW) ||
                        ($latestEvent->getEventType() == JobApplicationEvent::EVENT_SCHEDULE_SECOND_INTERVIEW)) {

                        if ($latestEvent->getStatus() != JobApplicationEvent::STATUS_INTERVIEW_FINISHED) {
                            $statusDate = LocaleUtil::getInstance()->formatDateTime($latestEvent->getEventTime());
                        } else {
                            $statusDate = $lang_Recruit_JobApplicationHistory_StatusFinished;
                        }
                    }
                }
        ?>
        <tr>
        	<td class="<?php echo $cssClass?>">
        		<?php
								if($allowDelete) {
									if (CommonFunctions::extractNumericId($applicationId) > 0) {
							?>
										<input type='checkbox' class='checkbox' name='chkID[]' value='<?php echo $applicationId ;?>' />
							<?php 	}
								} else {  ?>
									&nbsp;
							<?php 	}  ?>
        	</td>
            <td class="<?php echo $cssClass?>"> <a href="<?php echo $detailsURL. '&id=' . $applicationId; ?>"><?php echo CommonFunctions::escapeHtml($applicantName); ?></a></td>
            <td class="<?php echo $cssClass?>"><?php echo CommonFunctions::escapeHtml($app->getJobTitle()); ?></td>
            <td class="<?php echo $cssClass?>"><?php echo CommonFunctions::escapeHtml($app->getHiringManagerName()); ?></td>
            <td class="<?php echo $cssClass?>"><?php echo $status; ?>
            <?php if (!empty($statusDate)) { ?>
                <div class="eventDate">(<?php echo $statusDate; ?>)</div>
            <?php } ?>
            </td>
            <td class="<?php echo $cssClass?>">
            <?php
                $actions = $authManager->getAllowedActions($authorize, $app);

                foreach ($actions as $action) {
                    $resourceName = 'lang_Recruit_JobApplicationAction_' . $action;
                    $actionName = isset($$resourceName) ? $$resourceName : $action;
                    $actionURL = $baseURL . '&action=Confirm' . $action . '&id=' . $applicationId;
            ?>
                <a href="<?php echo $actionURL; ?>" style="white-space:nowrap;">
                    <?php echo $actionName;?>
                </a><br />
            <?php
                }
            ?>
            </td>
            <td class="<?php echo $cssClass?>">
                <a href="<?php echo $historyURL. '&id=' . $applicationId; ?>" style="white-space:nowrap;">
                    <?php echo $lang_Recruit_JobApplicationList_EventHistory; ?></a><br />
                <?php if(!empty($latestEvent)){ ?>    
            	<?php  if($latestEvent->getEventType() == JobApplicationEvent::EVENT_SCHEDULE_FIRST_INTERVIEW && $latestEvent->getStatus() != JobApplicationEvent::STATUS_INTERVIEW_FINISHED){ ?>   
                <a href="<?php echo $firstEvaluationURL. '&id=' . $applicationId; ?>" style="white-space:nowrap;">
                    <?php echo " Interview Assessment"; ?></a>
                 <?php }elseif($latestEvent->getEventType() == JobApplicationEvent::EVENT_SCHEDULE_SECOND_INTERVIEW && $latestEvent->getStatus() != JobApplicationEvent::STATUS_INTERVIEW_FINISHED){ ?>
                 	<a href="<?php echo $secondEvaluationURL. '&id=' . $applicationId; ?>" style="white-space:nowrap;">
                    <?php echo " Interview Assessment"; ?></a>   
               	 <?php } 
               	 
                 } ?>	
               	 
            </td>
        </tr>
        <?php
            }
        ?>
    </table>
    <?php } ?>
  </div>
  <script type="text/javascript">
        
        	if (document.getElementById && document.createElement) {
   	 			initOctopus();
			}
       
  </script>
 </form> 
</body>
</html>
